<script setup>
import router from "../../../../router/index.js";
import MyNav from "../../../../components/MyNav.vue";
import MyForm from "../../../../components/MyForm.vue";
import {RULE} from "../../../../const/index.js";
import {reactive, ref} from "vue";
import {insertApi} from "../../../../util/axios.js";

// 获取当前课程主键和课程标题
let insertPageParam = JSON.parse(sessionStorage.getItem('insertPageParam'));
let courseId = insertPageParam['courseId'];
let courseTitle = insertPageParam['courseTitle'];

/* ==================== 页面常量 ==================== */

// 路径导航
const navItems = [
  {icon: 'Notebook', label: '课程管理'},
  {icon: 'Notebook', label: '（' + courseTitle + '）的季次列表', url: '/Season'},
  {icon: 'Plus', label: '添加季次'},
];

/* ==================== 添加表单 ==================== */

// 表单项 + 表单值 + 表单规则
let items = ref([
  {label: '所属课程', prop: 'courseTitle', disabled: true},
  {label: '标题', prop: 'title', required: true, span: 12},
  {label: '排序', prop: 'idx', type: 'number', min: 1, required: true, span: 12},
  {label: '描述', prop: 'info', required: true, type: 'textarea'},
]);
let data = reactive({courseTitle, 'fkCourseId': courseId, info: '暂无描述'});
let rules = {
  title: RULE.TITLE,
  info: RULE.INFO,
};

/**
 * 添加成功后调用
 *
 * 1. 路由到 Season 页面
 */
function insertSuccess() {
  router.push('/Season');
}

</script>

<template>
  <my-nav :items="navItems"/>
  <el-divider/>
  <el-card class="season-insert-card" header="添加季次">
    <my-form type="insert"
             :items="items"
             :rules="rules"
             :args="{module: 'season'}"
             :api="insertApi"
             :params="data"
             :callback="insertSuccess"/>
  </el-card>
</template>

<style scoped lang="scss">
.season-insert-card {
  width: 70%; // 宽度
  margin: 20px auto 0; // 外边距
}
</style>